﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SalePartial.Utility;
using SalePartial.BLL.SAServiceRef;
using SalePartial.BLL;

public partial class SAClientWeb_EditStandardFrm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                RadDockLayout1.Visible = false;
                SABLL.ShowInfoMsg(this, "对不起，您权限不够！"); 
            }
            
            DoRefresh();
        }
    }


    protected void button_add_Click(object sender, EventArgs e)
    {
        if (!CheckInput()) return;

        StandardInfo newStandardInfo = new StandardInfo();
        newStandardInfo.Name = this.txtStandardName.Text;
        newStandardInfo.Description = this.txtStandardDescription.Text;

        bool ret = SABLL.AddStandard(newStandardInfo);

        if (!ret)
        {
            //SABLL.ShowErrMsg(this,"添加失败！" + SABLL.GetLastErrMsg());
            SABLL.ShowInfoMsg(this, "添加失败！标准名称不可重复");//添加失败提示简单的信息且将标准信息清空
            this.txtStandardName.Text = "";
            this.txtStandardDescription.Text = "";
            return;
        }
        else
        {
            SABLL.ShowInfoMsg(this, "添加成功！");
            DoRefresh();
        }
    }

    protected void listView_standardList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.listView_standardList.SelectedIndex < 0)
        {
            return;
        }
        //get current selected item
        //ListViewItem curItem = this.listView_standardList.Items;
        //if (curItem == null)
        //{
        //    return;
        //}

        ////get standard info. use standardName, becuase new added item, the tag is null.
        //String standardName = curItem.Text;
        //StandardInfo curStandardInfo = SABLL.GetStandardByName(standardName);

        List<StandardInfo> list = (List<StandardInfo>)this.listView_standardList.DataSource;
        StandardInfo curStandardInfo = list[this.listView_standardList.SelectedIndex];
        if (curStandardInfo == null)
        {
            SABLL.DoAlert(this); return;
        }

        this.txtStandardName.Text = curStandardInfo.Name;
        this.hidStandardId.Value = curStandardInfo.StandardId.ToString();
        this.txtStandardDescription.Text = curStandardInfo.Description;
    }

    protected void button_modify_Click(object sender, EventArgs e)
    {
        if (!CheckInput()) return;
        //get standardId.
        int standardId = Convert.ToInt32(this.hidStandardId.Value);
        StandardInfo updateStandardInfo = new StandardInfo();
        updateStandardInfo.StandardId = standardId;
        updateStandardInfo.Name = this.txtStandardName.Text;
        updateStandardInfo.Description = this.txtStandardDescription.Text;

        bool result = SABLL.UpdateStandard(updateStandardInfo);

        if (!result)
        {
            SABLL.DoAlert(this); return;
        }
        else
        {
            SABLL.ShowInfoMsg(this, "修改成功!");
            DoRefresh();
        }
    }

    protected void DoRefresh()
    {
        //List<StandardInfo> standInfoList = SABLL.GetAllStandards();
        //this.listView_standardList.DataSource = standInfoList;
        //this.listView_standardList.DataBind();

        this.listView_standardList.DataBind();
        this.txtStandardName.Text = "";
        this.txtStandardDescription.Text = "";
        this.hidStandardId.Value = null;
    }

    public string GetRowClickScript(object container) 
    {
        ListViewItem item = (ListViewItem)container;
        Control button = item.FindControl("SelectButton");
        return Page.ClientScript.GetPostBackEventReference(button, ""); 
    }

    protected void LbtnLink_Click(object sender, EventArgs e)
    {
         LinkButton lb = (LinkButton)sender;
         if (lb.CommandArgument != null && lb.CommandArgument != string.Empty)
         {
             String standardName = lb.CommandArgument;
             StandardInfo curStandardInfo = SABLL.GetStandardByName(standardName);
             if (curStandardInfo == null)
             {
                 SABLL.DoAlert(this); return;
             }

             this.txtStandardName.Text = curStandardInfo.Name;
             this.hidStandardId.Value = curStandardInfo.StandardId.ToString();
             this.txtStandardDescription.Text = curStandardInfo.Description;
         }
    }

    protected void button_delete_Click(object sender, EventArgs e)
    {
        //if (this.listView_standardList.SelectedIndex < 0)
        //    return;



        string standardName = this.txtStandardName.Text;

        //check whether there are families for this standard.
        //
        //int standardId = (int)this.listView_standardList.SelectedDataKey["StandardId"];
        int standardId = Convert.ToInt32(this.hidStandardId.Value);
        List<FamilyInfo> famInfos = SABLL.GetFamiliesByStandardId(standardId);
        if (famInfos != null && famInfos.Count > 0)
        {
            SABLL.ShowInfoMsg(this, String.Format("无法删除：[{0}]，仍有{1}个零部件属于标准：[{0}]！", standardName, famInfos.Count));
            return;
        }

        string msg = String.Format("确定要删除标准：{0}", standardName);

        bool result = SABLL.DeleteStandard(standardName);
        if (!result)
        {
            SABLL.DoAlert(this); return;
        }
        else
        {
            SABLL.ShowInfoMsg(this, "删除成功！");
            DoRefresh();
        }

    }

    protected void button_cancel_Click(object sender, EventArgs e)
    {
        //this.Close();
    }

    protected bool CheckInput()
    {
        if (String.IsNullOrEmpty(this.txtStandardName.Text.Trim()))
        {
            SABLL.ShowInfoMsg(this, @"标准的名字不能为空！");
            return false;
        }
        return true;
    }
  
}